package com.lzc.lblog.lblog.config;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzc.lblog.lblog.entity.MCategory;
import com.lzc.lblog.lblog.service.MCategoryService;
import com.lzc.lblog.lblog.service.MPostService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * 实现这两个接口的目的就是首页的分类信息我们放到应用级别里面去
 * ApplicationRunner应用启动的时候会执行该接口下的run方法
 * ServletContextAware应用上下文
 */
@Component
public class ContextStartup implements ApplicationRunner, ServletContextAware {

    private ServletContext servletContext;
    @Autowired
    private MCategoryService mCategoryService;
    @Autowired
    private MPostService postService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<MCategory> mCategories = mCategoryService.list(new LambdaQueryWrapper<MCategory>()
                .eq(MCategory::getStatus, 0));
        //放分类
        servletContext.setAttribute("categories",mCategories);
        //初始化本周热议
        postService.initWeek();

    }
  /**
     * 应用启动注入ServletContext对象
     * @param servletContext
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext=servletContext;
    }
}
